<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <!-- 加载 jQuery -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- 可选的 Bootstrap 主题文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <style>
        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .signup-container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        .signup-title {
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .required {
            color: red;
        }
        .btn-container {
            display: flex;
            justify-content: space-between;
        }
    </style>
</head>
<body>
    <div class="signup-container">
        <div class="signup-title">用户注册</div>
        <!-- 使用 Bootstrap 的表单样式 -->
        <form>
            <div class="form-group">
                <label for="username"><span class="required">*</span> 用户名</label>
                <input type="text" class="form-control" id="username" placeholder="请输入用户名" required>
            </div>
            <div class="form-group">
                <label for="password"><span class="required">*</span> 密码</label>
                <input type="password" class="form-control" id="password" placeholder="请输入密码" required>
            </div>
            <div class="form-group">
                <label for="confirm-password"><span class="required">*</span> 确认密码</label>
                <input type="password" class="form-control" id="confirm-password" placeholder="请再次输入密码" required>
            </div>
            <div class="btn-container">
                <button type="button" class="btn btn-primary btn-block" onclick="onSignup()">注册</button>
                <!-- 跳转到登录页面 -->
                <button type="button" class="btn btn-default btn-block" onclick="window.location.href='/static/view/signin.html'">返回登录</button>
            </div>
        </form>
    </div>

    <script>
        function onSignup() {
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;
            var confirmPassword = document.getElementById('confirm-password').value;

            // 客户端验证
            if (!username || !password || !confirmPassword) {
                alert("请输入用户名、密码和确认密码！");
                return;
            }
            if (password !== confirmPassword) {
                alert("两次输入的密码不一致！");
                return;
            }

            $.ajax({
                url: "/static/view/signin.html",
                type: "POST",
                data: {
                    "username": username,
                    "password": password
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    if (textStatus === "error") {
                        alert("错误：" + errorThrown);
                    } else {
                        alert("请求失败：" + textStatus);
                    }
                },
                success: function (body, textStatus, jqXHR) {
                    var resp = JSON.parse(body);
                    alert("注册成功！");
                    localStorage.setItem("token", resp.data.Token);
                    localStorage.setItem("username", resp.data.Username);
                    window.location.href = resp.data.Location || "/static/view/signin.html";
                }
            });
        }
    </script>
</body>
</html>